package cn.tedu.blog.server.controller.system;


import cn.tedu.blog.server.pojo.dto.RoleAddNewDTO;
import cn.tedu.blog.server.pojo.vo.RoleListVO;
import cn.tedu.blog.server.service.IRoleService;
import cn.tedu.blog.server.utils.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 雷茂林
 * @description: 后台系统角色管理控制器
 * @date
 */
@Api(tags = "2. 角色管理")
@Slf4j
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @ApiOperation("创建角色")
    @ApiOperationSupport(order = 100)
    @PostMapping("/creatRole")
    @PreAuthorize("hasAuthority('/system/rol/creatRole')")
    public JsonResult creatRole(@RequestBody RoleAddNewDTO roleAddNewDTO){
        log.debug("接收到客户端提交的请求，参数：{}",roleAddNewDTO);
        roleService.creatRole(roleAddNewDTO);
        return JsonResult.ok();
    }



    @ApiOperation("删除角色")
    @ApiOperationSupport(order = 200)
    @PostMapping("/deleteById")
    @PreAuthorize("hasAuthority('/system/role/deleteById')")
    public JsonResult deleteById(@RequestBody RoleListVO roleListVO){
        log.debug("接收到客户端提交的请求，参数：{}",roleListVO.getId());
        roleService.deleteById(roleListVO.getId());
        return JsonResult.ok();
    }



    @ApiOperation("批量删除角色")
    @ApiOperationSupport(order = 201)
    @PostMapping("/deleteByIds")
    @PreAuthorize("hasAuthority('/system/role/deleteByIds')")
    public JsonResult deleteByIds(@RequestBody List<Long> ids){
        log.debug("接收到客户端提交的请求，参数：{}",ids);
        roleService.deleteByIds(ids);
        return JsonResult.ok();
    }



    @ApiOperation("修改角色")
    @ApiOperationSupport(order = 300)
    @PostMapping("/updateById")
    @PreAuthorize("hasAuthority('/system/rol/updateById')")
    public JsonResult updateById(@RequestBody RoleAddNewDTO roleAddNewDTO){
        log.debug("接收到客户端提交的请求，参数：{}",roleAddNewDTO);
        roleService.updateById(roleAddNewDTO);
        return JsonResult.ok();
    }



    @ApiOperation("根据角色id回显角色信息")
    @GetMapping("/getBackFillById")
    public RoleListVO getBackFillById(Long id){
        log.debug("接收到客户端数据回填的请求，参数：{}",id);
        return roleService.getBackFillById(id);
    }



    @ApiOperation("根据用户id回显拥有的角色id")
    @GetMapping("/getBackFillByUserId")
    public Long[] getBackFillByUserId(Long userId){
        log.debug("接收到客户端提交的【根据用户id回显该用户拥有的角色名称】请求，参数：{}",userId);
        return roleService.getBackFillByUserId(userId);
    }



    @ApiOperation("回显角色列表")
    @GetMapping("/getRoleEcho")
    public List<RoleListVO> getRoleEcho(){
        log.debug("接收到客户端角色回显的请求");
        return roleService.getRoleEcho();
    }



    @PostMapping("/getCountAll")
    public int getCountAll(@RequestBody RoleListVO roleListVO){
        log.debug("接收到客户端提交的请求，参数：{}",roleListVO);
        return roleService.getCountAll(roleListVO);
    }



    @ApiOperation("条件分页查询角色列表")
    @ApiOperationSupport(order = 400)
    @PostMapping("/listByKeyword")
    public List<RoleListVO> listByKeyword(@RequestBody RoleListVO roleListVO){
        log.debug("接收到客户端提交的请求，参数：{}",roleListVO);
        return roleService.listByKeyword(roleListVO);
    }



    @PostMapping("/roleMenuById")
    public List<Long> roleMenuById(@RequestBody RoleAddNewDTO roleAddNewDTO){
        log.debug("接收到客户端提交的请求【根据角色id查询角色权限】，参数：{}",roleAddNewDTO.getId());
        return roleService.roleMenuById(roleAddNewDTO.getId());
    }



    // 赋予角色权限
    @PostMapping("/updateMenuByRoleId/{roleId}")
    @PreAuthorize("hasAuthority('/system/rol/updateMenuByRoleId')")
    public JsonResult updateMenuByRoleId(
            @RequestBody Long[] menuTreeArray,@PathVariable("roleId") Long roleId){
        log.debug("接收到客户端提交的请求【修改角色权限】，参数：【di={}】，list=[{}]",roleId,menuTreeArray);
        roleService.updateMenuByRoleId(roleId,menuTreeArray);
        return JsonResult.ok();
    }

}
